package bookStore.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookStore.po.User;
import bookStore.service.privilege.UserService;
import bookStore.service.privilege.UserServiceImpl;
import bookStore.tool.Constants;
import bookStore.tool.UploadTool;
import bookStore.tool.UrlUtil;
import bookStore.vo.UserWithResources;

/**
 * Servlet implementation class UserController
 */
@WebServlet("/user/*")
@MultipartConfig
public class UserController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String url;
	private String traget = "";
	UserService userService = new UserServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		url = UrlUtil.getUrl(request.getRequestURI());
		Map<String, String> parametersMap;
		String username=null;
		String password=null;
		switch(url) {
		
		case "login":
			User currentUser=null;
			username=request.getParameter("username");
			password= request.getParameter("password");
			String userIdString = request.getParameter("userId");
			if(userIdString!=null) {
				currentUser = userService.get(Integer.parseInt(userIdString));
			}else {
				currentUser = userService.login(username, password);
			}
			if(currentUser!=null) {
				request.getSession().setAttribute("currentUser", currentUser);
				request.getSession().setAttribute("blackList", userService.getBlackList(currentUser.getId()));
				setTraget("/index.jsp");
			}else {
				setTraget("/loginForm.jsp");
			}
			break;
			
		case "register":
			username=request.getParameter("username");
			password= request.getParameter("password");
			User user = new User();
			user.setUsername(username);
			user.setPassword(password);
			String pictureName = UploadTool.uploadFile(Constants.PICTURES_HOME, "pictureFile", request);
			user.setPicture(Constants.PICTURES_PATH+pictureName);
			int userId=userService.register(user);
			setTraget("login?userId="+userId);
			break;
		case "logout":
			request.getSession().removeAttribute("currentUser");
			request.getSession().removeAttribute("blackList");
			setTraget("/index.jsp");
			break;
		}
		
		
		if(traget.endsWith(".jsp")) {
			request.getRequestDispatcher(traget).forward(request, response);
		}else {
			response.sendRedirect(traget);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	protected String getTraget() {
		return traget;
	}

	protected void setTraget(String traget) {
		this.traget = traget;
	}

	
}
